//
//  BEHomeController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/7/26.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import SnapKit
import SDCycleScrollView


class BEHomeController: BEBaseViewController,SDCycleScrollViewDelegate {
    
    var topScrollView: SDCycleScrollView!
    
    var imagesURLStrings: NSArray = {
        //美国版
        let imagesURLStrings = [
            "http://www.globaloiler.com/images/forapp/1.jpg",
            "http://www.globaloiler.com/images/forapp/2.jpg",
            "http://www.globaloiler.com/images/forapp/3.jpg",
            "http://www.globaloiler.com/images/forapp/4.jpg",
            "http://www.globaloiler.com/images/forapp/5.jpg"
        ]
        return imagesURLStrings as NSArray
    }()
    
    var localizationImageNamesGroup: [String] = {
        let imagesNameStrings = [
            "cyc_1.jpg",
            "cyc_2.jpg",
            "cyc_3.jpg",
            "cyc_4.jpg",
            "cyc_5.jpg"
        ]
        return imagesNameStrings as [String]
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = COLOR_ALL_BG
        setupUI()
    }
    
    private func setupUI(){
        
        automaticallyAdjustsScrollViewInsets = false
        topScrollView = SDCycleScrollView(frame: CGRect.zero, delegate: self, placeholderImage: UIImage(named: "cyc_2"))
        topScrollView.backgroundColor = COLOR_ALL_BG
        
        // 网络加载图片的轮播器
//        topScrollView.imageURLStringsGroup = imagesURLStrings as [AnyObject]
        topScrollView.localizationImageNamesGroup = localizationImageNamesGroup
        topScrollView.autoScrollTimeInterval = 3
        view.addSubview(topScrollView)
        
        let hireBtn = createBtn(title: "Hire a Professional", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "hire-a-professional", target: self, action: #selector(hireBtnClick))
        
        let techsBtn = createBtn(title: "Our Techs", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "our-techs", target: self, action: #selector(techsBtnClick))
        
        let advantBtn = createBtn(title: "Our Advantages", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "our-advantages", target: self, action: #selector(advantBtnClick))
        
        view.addSubview(hireBtn)
        view.addSubview(techsBtn)
        view.addSubview(advantBtn)
        
        hireBtn.layer.cornerRadius = 5
        hireBtn.layer.masksToBounds = true
        techsBtn.layer.cornerRadius = 5
        techsBtn.layer.masksToBounds = true
        advantBtn.layer.cornerRadius = 5
        advantBtn.layer.masksToBounds = true
        
        topScrollView.snp.makeConstraints { (make) in
            make.width.equalTo(SCREEN_WIDTH)
            make.top.equalTo(64)
            make.height.equalTo(200)
        }
        
        hireBtn.snp.makeConstraints { (make) in
            make.top.equalTo(topScrollView.snp.bottom).offset(5)
            make.left.equalTo(5)
            make.right.equalTo(-5)
        }
        
        //        var heightScale :CGFloat = 0
        //
        //        switch iPhoneModel.getCurrentModel() {
        //        case .iPhone6:
        //            heightScale = 0.65
        //        case .iPhone6p:
        //            heightScale = 0.7
        //        default:
        //            heightScale = 0.65
        //        }
        //
        techsBtn.snp.makeConstraints { (make) in
            make.top.equalTo(hireBtn.snp.bottom).offset(5)
            make.left.equalTo(5)
            make.bottom.equalTo(-53)
            make.height.equalTo(hireBtn.height*0.68)
        }
        
        advantBtn.snp.makeConstraints { (make) in
            make.top.equalTo(hireBtn.snp.bottom).offset(5)
            make.right.equalTo(-5)
            make.bottom.equalTo(-53)
            make.height.width.equalTo(techsBtn)
            make.left.equalTo(techsBtn.snp.right).offset(5)
        }
    }
    
    
    //便利的创建btn的方法
    func createBtn(title:String?,normalColor:UIColor,highlightedColor:UIColor,fontSize:CGFloat,backgroundImageName:String?,target: AnyObject?, action: Selector) -> UIButton{
        
        let button = UIButton();
        
        button.setTitle(title, for: .normal)
        button.setTitleColor(normalColor, for: .normal)
        button.setTitleColor(highlightedColor, for: .highlighted)
        
        button.titleLabel!.font = baseFont(size: fontSize)
        
        button.addTarget(target, action: action, for: .touchUpInside)
        
        if let str = backgroundImageName {
            button.setBackgroundImage(UIImage(named:str), for: .normal)
            let hightStr = str.appending("_highlighted")
            button.setBackgroundImage(UIImage(named:hightStr), for: .highlighted)
        }
        button.adjustsImageWhenHighlighted = false
        button.sizeToFit()
        return button
    }
    
    
    @objc private func hireBtnClick(){
        if !BEAccountModel.isLogin(){
            let alertVC = UIAlertController(title: "Please login first!", message: "Log in before you can view the orders", preferredStyle: .alert)
            let cancleAction = UIAlertAction(title: "Later", style: .cancel) { (_) in
                
            }
            let doneAction = UIAlertAction(title: "Login Now", style: .default) { (_) in
                _ = isLogin()
            }
            alertVC.addAction(cancleAction)
            alertVC.addAction(doneAction)
            
            UIApplication.shared.keyWindow?.rootViewController!.present(alertVC, animated: true, completion: nil)
            return
        }
        let hireController = UIStoryboard(name: "BENewOrderController", bundle: nil).instantiateInitialViewController()!
        navigationController?.pushViewController(hireController, animated: true)
    }
    
    
    @objc private func techsBtnClick(){
        if isLogin() == false{
            return
        }
        let techListController = BETechsListController()
        navigationController?.pushViewController(techListController, animated: true)
    }
    
    @objc private func advantBtnClick(){
        
        let ourAdvan = UIStoryboard(name: "BEOurAdvantageVc", bundle: nil).instantiateInitialViewController()!
        navigationController?.pushViewController(ourAdvan, animated: true)
    }
}
